package com.be.beadmin.device.controller;

import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.be.beadmin.common.entity.PageEntity;
import com.be.beadmin.device.dto.output.DeInfoExcelEx;
import com.be.beadmin.device.entity.DeInfo;
import com.be.beadmin.device.dto.output.DeInfoEx;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Page;

import com.be.beadmin.device.service.DeInfoService;
import com.be.beadmin.device.dto.input.DeInfoQueryCriteria;
import com.be.beadmin.device.dto.input.DeInfoDto;
import org.springframework.data.domain.Pageable;
import com.be.beadmin.common.utils.ResultModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;


import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

import com.alibaba.excel.EasyExcel;

/**
* @author chenxu
* @date 2023-09-09
**/
@RestController
public class DeInfoController {

    @Autowired
    private  DeInfoService deInfoService;


    @PostMapping(value = "/device/getDeInfoByPage")
    public ResultModel getDeInfoByPage(@RequestBody PageEntity pageEntity)  {
        return ResultModel.success(deInfoService.getDeInfoByPage(pageEntity)) ;
    }

    @PostMapping("/device/addDeInfo")
    public ResultModel addDeInfo(@RequestBody DeInfoDto deInfoDto){
        deInfoService.addDeInfo(deInfoDto);
        return ResultModel.success(ResultModel.ADD);
    }
    @PostMapping("/device/delDeInfo")
    public ResultModel delDeInfo(@RequestBody DeInfoDto deInfoDto){
        deInfoService.delDeInfo(deInfoDto.getId());
        return ResultModel.success(ResultModel.DELETE);
    }


    @PostMapping("/device/updateDeInfo")
    public ResultModel updateDeInfo(@RequestBody DeInfoDto deInfoDto){
        deInfoService.updateDeInfo(deInfoDto);
        return ResultModel.success(ResultModel.UPDATE);
    }


    @PostMapping("/device/exportDeInfo")
    public void exportDeInfo(HttpServletResponse response ,@RequestBody PageEntity pageEntity) throws IOException {
        Page<DeInfoEx> deInfoPage = deInfoService.getDeInfoByPage(pageEntity);
        List<DeInfoEx> deInfoExes = deInfoPage.getContent();
        List<DeInfoExcelEx> deInfoExcelExes=deInfoExes.stream().map(item->{
           DeInfoExcelEx excel=new DeInfoExcelEx();
            BeanUtils.copyProperties(item,excel);
            return excel;
        }).collect(Collectors.toList());
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("DeInfo接口", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xls");
        //EasyExcel.write(response.getOutputStream(), DeInfoExcelEx.class).sheet("sheet1").doWrite(deInfoExcelExes);
        ExcelWriterBuilder ewb=EasyExcel.write(response.getOutputStream(), DeInfoExcelEx.class);
        ewb.excelType(ExcelTypeEnum.XLS);
        ewb.sheet("sheet1").doWrite(deInfoExcelExes);
    }

    @ApiOperation("获取所有设备编号")
    @PostMapping("/device/getDeIds")
    public ResultModel getDeviceIds(){
        return ResultModel.success(deInfoService.getDeIds());
    }
}